<?php

/**
 * 携程  每天跑1次
 * @author lzp300
 * @copyright 2015-03-26
 */

class toQmgHotel extends actionAbstract{
    
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
	    set_time_limit(0);
    }
	
	//获取可售酒店
	function index(){
		$this->loadApi('orderapi');
		$orderapi = new orderapi();
		$GetImmediateHotelList=$orderapi->GetImmediateHotelListTO();
		
		$this->loadModel('xc','uphotel');
		$noHotelArray="";//下架酒店
		$re=$this->xc->uphotelModel->fetchAll("select hid from xc_uphotel where status=1");
		foreach($re as $k=>$v){
			if(!in_array($v['hid'],$GetImmediateHotelList)){$noHotelArray[]=$v['hid'];}
		}
		
		if($noHotelArray){$this->closHotel($noHotelArray);}//关闭下架酒店 房态
	}
	
	//关闭下架酒店 房态
	function closHotel($noHotelArray=array()){
		$this->loadModel('qm','room');
		$this->loadModel('qm','roombatch');
		$noHotelStr="";
		foreach($noHotelArray as $k=>$v){
			$noHotelStr.="'".$v."',";
		}
		$noHotelStr=trim($noHotelStr,',');
		
		//更新酒店状态
		$this->loadModel('xc','uphotel');
		$data['status']=0;$data['created']=date('Y-m-d H:i:s',time());
		$this->xc->uphotelModel->update($data,"hid in (".$noHotelStr.")");
		
		//查询下架酒店ID 房型ID
		$rooms=$this->qm->roomModel->fetchAll("select hid,rid from qm_room where hid in (".$noHotelStr.")");
		foreach($rooms as $k=>$v){
			$sql="select maxNum,date,roomStatus from qm_roombatch where hid='".$v['hid']."' and rid='".$v['rid']."' and date>='".date('Y-m-d',time())."' order by date asc";
			$re=$this->qm->roombatchModel->fetchAll($sql);
			$this->HotelStatus($re,$v['hid'],$v['rid']);
		}
	}
	
	//房态关闭
	function HotelStatus($array=array(),$hid=0,$rid=0){
		$this->loadApi('toQmgHotelLog');
		$toQmgHotelLog = new toQmgHotelLog();
		
		$body['startDate']=$body['endDate']="";
		$body['hotelCode']=$hid;
		$body['roomID']=$rid;
		$body['sourceId']=2;
		$body['editer']='qmg';$body['roomInfoList']=array();
		if($array){
			$body['startDate']=$array[0]['date'];
			$body['endDate']=$array[(count($array)-1)]['date'];
			
			$timeDa=$timeDb=$arrSort=array();$count=(strtotime($body['endDate'])-strtotime($body['startDate']))/24/3600;
			for($j=0;$j<$count;$j++){
				$timeDa[]=date('Y-m-d',(strtotime($body['startDate'])+$j*24*3600));
			}
			foreach($array as $k=>$v){$timeDb[]=$v['date'];}
			foreach($timeDa as $k=>$v){
				if(!in_array($v,$timeDb)){
					$count++;
					$array[$count]['maxNum']=0;
					$array[$count]['date']=$v;
					$array[$count]['roomStatus']=0;
				}
			}
			foreach($array AS $uniqid => $row){$arrSort[] = $row['date'];}
			array_multisort($arrSort, SORT_ASC, $array);
			
			$i=0;
			foreach($array as $k=>$v){
				$body['roomInfoList'][$i]['fl']=$v['maxNum'];
				$body['roomInfoList'][$i]['setToDate']=$v['date'];
				$body['roomInfoList'][$i]['changeDefault']='T';
				$body['roomInfoList'][$i]['holdDeadline']='9999';
				$body['roomInfoList'][$i]['note']='';
				$body['roomInfoList'][$i]['reserveTime']='0';
				$body['roomInfoList'][$i]['roomstatus']='N';
				$body['roomInfoList'][$i]['roomsInv']='0';
				$body['roomInfoList'][$i]['guarantee']='';
				$body['roomInfoList'][$i]['guaranteeLCT']=4;
				$body['roomInfoList'][$i]['prepayLCT']=23988;
				$body['roomInfoList'][$i]['allNeedGuarantee']='F';
				$body['roomInfoList'][$i]['lateReserveTime']=0;
				$body['roomInfoList'][$i]['deductType']='C';
				$body['roomInfoList'][$i]['blanceType']='PP';
				$body['roomInfoList'][$i]['restorable']='F';
				$body['roomInfoList'][$i]['checkType']='C';
				$body['roomInfoList'][$i]['userLimited']='9999';
				$i++;
			}
			$toQmgHotelLog->roomstatus($body);
		}
	}
	
}